package main

import (
	. "leetcode/linkedlist"
)

func removeElements(head *ListNode, val int) *ListNode {
	leading := &ListNode{Next: head}
	for pre, cur := leading, head; cur != nil; cur = cur.Next {
		if cur.Val == val {
			pre.Next = cur.Next
		} else {
			pre = cur
		}
	}
	return leading.Next
}

func main() {
	head := CreateList([]int{1, 2, 3, 4})
	TraverseList(head)
	head = removeElements(head, 3)
	TraverseList(head)
	head = removeElements(head, 1)
	TraverseList(head)
	head = removeElements(head, 5)
	TraverseList(head)
	head = CreateList([]int{1, 1, 2})
	head = removeElements(head, 1)
	TraverseList(head)
}
